using System;
using Microsoft.SPOT;
using System.Net;
using System.IO;
using System.Threading;

namespace PachubeDataAccess
{
    public class Feed
    {
        public Account AssociatedAccount;
        public int Id;

        public Feed(int id, Account account)
        {
            Id = id;
            AssociatedAccount = account;
        }

        public string Get()
        {
            String result = "";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(AssociatedAccount.EventsUrl + this.Id.ToString());
            req.Method = "GET";
            req.ContentType = "application/json";
            req.UserAgent = "NetduinoPlus";
            req.Headers.Add("X-PachubeApiKey", AssociatedAccount.ApiKey);

            try
            {
                WebResponse resp = req.GetResponse();

                using (Stream s = resp.GetResponseStream())
                {
                    byte[] data = new byte[s.Length];
                    s.Read(data, 0, (int)s.Length);
                    result = new string(System.Text.Encoding.UTF8.GetChars(data));
                }
            }
            catch (Exception e)
            {
                Debug.Print(e.Message);
            }
            return result;
        }

        public void Post(IFeedItem item)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(this.AssociatedAccount.EventsUrl + this.Id.ToString());
            req.Method = "PUT";
            req.ContentType = "application/json";
            req.UserAgent = "NetduinoPlus";
            req.Headers.Add("X-PachubeApiKey", this.AssociatedAccount.ApiKey);
            req.Timeout = 1000;
            if (this.AssociatedAccount.HttpProxy != null)
                req.Proxy = this.AssociatedAccount.HttpProxy;

            string content = item.ToJson(this.Id);
            Debug.Print(content);


            byte[] postdata = System.Text.Encoding.UTF8.GetBytes(content);
            req.ContentLength = postdata.Length;

            try
            {
                using (Stream s = req.GetRequestStream())
                {
                    s.Write(postdata, 0, postdata.Length);
                }

                using (WebResponse resp = req.GetResponse())
                {

                    using (Stream respStream = resp.GetResponseStream())
                    {
                        byte[] respBytes = new byte[respStream.Length];

                        int length = respStream.Read(respBytes, 0, respBytes.Length);

                        string respString = new string(System.Text.Encoding.UTF8.GetChars(respBytes));
                        Debug.Print(respString);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.Print("exception : " + ex.Message);
            }
            finally
            {
                req.Dispose();
            }
        }
        
    }
}
